import configparser
import os
import logger
from pyAgDongle import PyAgDongle2, PyAgDongle
from PyQt5.QtWidgets import QMessageBox

class DongleConfig():
    def __init__(self):
        self.log = logger.logger_config(log_path="DongleConfig.log", logging_name='加密狗日志')
        config_file_path = os.path.join(os.path.dirname(__file__), 'DongleConfig.ini')
        self.config = configparser.ConfigParser()

        self.agDongle = PyAgDongle()
        self.agDongle2 = PyAgDongle2()

        try:
            self.config.read(config_file_path)
            self.registercode = self.config.get('Dongle', 'registercode').replace('-','')
            self.port = "COM"+self.config.get('SerialPort', 'port')
            self.baudrate = int(self.config.get('SerialPort', 'baudrate'))
            self.log.info(self.registercode)
        except:
            self.log.error("读取DongleConfig失败")

    def save_port(self, portNum):
        portNum = portNum.replace('COM', '')
        self.config.set('SerialPort', 'port', portNum)
        with open('DongleConfig.ini', 'w') as configfile:
            self.config.write(configfile)
        print('保存port成功')

    ############################# 软件加密狗 ############################
    def init_dongle2(self):
        self.agDongle2.py_agCreateCAgDongle2()
        self.machine_code,self.machine_id = self.agDongle2.py_agGenerateMRCode_DR2()   

    def check_dongle2_validate(self):
        self.Validate = self.agDongle2.py_agAuthorityValidate2() # 授权剩余天数
        self.log.debug(f'validate2-1:{self.Validate}')
        if self.Validate < 0:
            #尝试激活
            r = self.agDongle2.py_agActivateSoftware2(self.registercode)
            if not r:
                self.log.info('软件加密狗激活完成')
            else:
                self.log.info(f'软件加密狗激活失败，错误码{r}')
            self.Validate = self.agDongle2.py_agAuthorityValidate2() # 再获取授权剩余天数
            self.log.debug(f'validate2-2:{self.Validate}')

            if self.Validate < 0:
                if self.Validate == -34:
                    self.log.error(f'软件加密狗过期，请重新授权！机器码：{self.machine_code}，机器ID：{self.machine_id}')
                    QMessageBox.warning(None, '错误', f'软件加密狗过期，请重新授权！\r\n机器码：{self.machine_code}\r\n机器ID：{self.machine_id}')
                else:
                    self.log.error(f'验证软件加密狗错误！错误码：{self.Validate}，机器码：{self.machine_code}，机器ID：{self.machine_id}')
                    QMessageBox.warning(None, '错误', f'验证软件加密狗错误！错误码：{self.Validate}\r\n机器码：{self.machine_code}\r\n机器ID：{self.machine_id}')
                return 0
            else:
                self.log.info(f'软件加密狗验证通过，将在{self.Validate}天后过期！')
                return 1 
        else:
            self.log.info(f'软件加密狗验证通过，将在{self.Validate}天后过期！')
            return 1

        
    def start_activate_check(self):
        r = self.agDongle2.py_agActivateSoftware2(self.registercode)
        return r

    def close_dongle2(self):
        self.agDongle2.py_agDeleteCAgDongle2()
        self.log.info('close dongle2')    

    ############################## 硬件加密狗 ########################
    def is_hardDongle(self):
        isExist = self.agDongle.py_agCreateCAgDongle()
        self.log.debug(f'isExist:{isExist}')#存在就是0
        if not isExist:
            self.log.info('硬件加密狗读取成功！')
            self.machine_code = self.agDongle.py_agGenerateMRCode_DR()
            self.machine_id = self.agDongle.py_agGetLocalMachineID()
            return 1
        else:
            self.log.info('硬件加密狗读取失败！')
            return 0
        
    def check_hardDongle_validate(self):
        self.Validate = self.agDongle.py_agAuthorityValidate() # 授权剩余天数
        self.log.debug(f'validate1:{self.Validate}')
        if self.Validate < 0:
            #尝试激活
            r = self.agDongle.py_agActivateSoftware(self.registercode)
            if not r:
                self.log.info('硬件加密狗激活完成')
            else:
                self.log.info(f'硬件加密狗激活失败，错误码{r}')
            self.Validate = self.agDongle.py_agAuthorityValidate() # 再获取授权剩余天数
            self.log.debug(f'validate2:{self.Validate}')
            if self.Validate < 0:
                if self.Validate == -34:
                    self.log.error(f'硬件加密狗过期，请重新授权！机器码：{self.machine_code}，机器ID：{self.machine_id}')
                    QMessageBox.warning(None, '错误', f'硬件加密狗过期，请重新授权！\r\n机器码：{self.machine_code}\r\n机器ID：{self.machine_id}')
                else:
                    self.log.error(f'验证硬件加密狗错误！错误码：{self.Validate}，机器码：{self.machine_code}，机器ID：{self.machine_id}')
                    QMessageBox.warning(None, '错误', f'验证硬件加密狗错误！错误码：{self.Validate}\r\n机器码：{self.machine_code}\r\n机器ID：{self.machine_id}')
                return 0
            else:
                self.log.info(f'硬件加密狗验证通过，将在{self.Validate}天后过期！')
                return 1 
        else:
            self.log.info(f'硬件加密狗验证通过，将在{self.Validate}天后过期！')
            return 1
        
    def close_hardDongle(self):
        self.agDongle.py_agDeleteCAgDongle()
        self.log.info('close harddongle')

    def delete_dongles(self):
        self.agDongle.py_agDeleteCAgDongle()
        self.log.info('close harddongle')
        self.agDongle2.py_agDeleteCAgDongle2()
        self.log.info('close dongle2')  

if __name__ == '__main__':
    a = DongleConfig()
